package com.gok.seasonmovie.controller;

import com.gok.seasonmovie.entity.Movie;
import com.gok.seasonmovie.service.MovieService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class MovieController {
    @Resource
    private MovieService movieService;

    @GetMapping("/movies")
    public Map<String, Object> getShowingMovies() {
        try{
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("msg", "ok");
            List<Movie> movies = movieService.findAll();
            response.put("data", movies);
            return response;
        }catch (RuntimeException e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 600);
            errorResponse.put("msg", "Database error: " + e.getMessage());
            return errorResponse;
        }
    }

    @PostMapping("/getMovieByMid")
    public Map<String, Object> getMovieByMid(@RequestParam String id){
        try{
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("msg", "ok");
            List<Movie> movies = movieService.getMovieByMid(id);
            response.put("data", movies);
            return response;
        }catch (RuntimeException e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 600);
            errorResponse.put("msg", "Database error: " + e.getMessage());
            return errorResponse;
        }

    }

    @PostMapping("/getsearchMovie")
    public Map<String, Object> searchMovies(@RequestParam("keyword") String keyword) {
        try{
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("msg", "ok");
            List<Movie> movies = movieService.searchMovies(keyword);
            response.put("data", movies);
            return response;
        }catch (RuntimeException e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 600);
            errorResponse.put("msg", "Database error: " + e.getMessage());
            return errorResponse;
        }
    }

}